public class Test {

    public static String formatString (String str, char[] arg){
        StringBuilder stringBuilder = new StringBuilder();
        String tmp = "";
        String tmp2="";
        int j =0;
        if (str.length() == 0) {
            tmp2 =  String.valueOf(arg);
            tmp2 = tmp2.substring(0, arg.length);
            stringBuilder.append(tmp2);
            return String.valueOf(stringBuilder);
        }
        for(int i = 0;i<str.length();i++){
              if(i==str.length()-1){
                  stringBuilder.append(str.charAt(i));
                  if(j<arg.length){
                   tmp2=  String.valueOf(arg);
                     tmp2= tmp2.substring(j,arg.length);
                     stringBuilder.append(tmp2);
                  }
                  return String.valueOf(stringBuilder);
              }
            tmp=str.substring(i,i+2);
            if(tmp.equals("%s")&&(i+1)<str.length()){

                stringBuilder.append(arg[j]);
                j++;
                i++;

            }else{
                stringBuilder.append(str.charAt(i));
            }
        }
        String outcome= String.valueOf(stringBuilder);
        return outcome;
    }

    public static void main(String[] args) {
        String s1 = "";
        char[] arr  = new char[]{'B','D','F'};
        String s2 = formatString(s1,arr);
        System.out.println(s2);
    }
}
